package com.bwf.lgqingdan.gui.view;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by LG on 2016/11/14.
 * Tips:不同字体颜色的TextView封装
 */

public class ColorSpanTextView extends TextView{
    public ColorSpanTextView(Context context) {
        super(context);
    }
    public ColorSpanTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ColorSpanTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private SpannableStringBuilder builder;
    private String spannableString;
    public ColorSpanTextView spannableString(String text){
        builder = new SpannableStringBuilder(text);
        spannableString = text;
        return this;
    }
    public ColorSpanTextView span(int color,int start,int end){
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        builder.setSpan(colorSpan,start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }
    public ColorSpanTextView span(int color,String subText){
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        int start = spannableString.indexOf(subText);
        int end = start+subText.length();
        builder.setSpan(colorSpan,start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }
    public void set(){
        setText(builder);
    }
}
